package org.ktx.osr.data

import java.io.File
import java.util.UUID

/**
 * 音视频处理任务数据类
 */
data class MediaProcessingTask(
    val id: String = UUID.randomUUID().toString(),
    val videoFile: File,
    val audioFile: File?,
    val outputFile: File,
    val status: TaskStatus = TaskStatus.PENDING,
    val progress: Int = 0,
    val errorMessage: String? = null,
    val createdAt: Long = System.currentTimeMillis(),
    val startedAt: Long? = null,
    val completedAt: Long? = null
) {
    /**
     * 任务状态枚举
     */
    enum class TaskStatus {
        PENDING,    // 等待处理
        PROCESSING, // 正在处理
        COMPLETED,  // 处理完成
        FAILED,     // 处理失败
        UPLOADING,  // 正在上传
        UPLOADED    // 上传完成
    }
    
    /**
     * 更新任务状态
     */
    fun updateStatus(newStatus: TaskStatus): MediaProcessingTask {
        return copy(
            status = newStatus,
            startedAt = if (newStatus == TaskStatus.PROCESSING && startedAt == null) System.currentTimeMillis() else startedAt,
            completedAt = if (newStatus in listOf(TaskStatus.COMPLETED, TaskStatus.FAILED)) System.currentTimeMillis() else completedAt
        )
    }
    
    /**
     * 更新进度
     */
    fun updateProgress(newProgress: Int): MediaProcessingTask {
        return copy(progress = newProgress.coerceIn(0, 100))
    }
    
    /**
     * 设置错误信息
     */
    fun setError(errorMessage: String): MediaProcessingTask {
        return copy(
            status = TaskStatus.FAILED,
            errorMessage = errorMessage,
            completedAt = System.currentTimeMillis()
        )
    }
    
    /**
     * 获取任务描述
     */
    fun getDescription(): String {
        return "处理任务: ${videoFile.name}"
    }
    
    /**
     * 获取处理时长（毫秒）
     */
    fun getProcessingDuration(): Long? {
        return if (startedAt != null && completedAt != null) {
            completedAt - startedAt
        } else null
    }
}
